package com.itheima.stock.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.common.domain.*;
import com.itheima.stock.pojo.StockBusiness;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.resp.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author byzhao
 * @version 1.0
 * @description 股票表现层
 * @date 2022/10/12 16:08:18
 */
@Slf4j
@RestController
@RequestMapping(path = "/api/quot")
public class StockController {

    @Autowired
    private StockService stockService;

    @GetMapping(path = "/stock/business/all")
    public List<StockBusiness> getAllStockBusinesses() {
        return stockService.getAllStockBusiness();
    }

    /**
     * 获取大盘指数数据
     *
     * @return
     */
    @GetMapping("/index/all")

    public R<List<InnerMarketDomain>> innerIndexAll() {
        return stockService.innerIndexAll();
    }

    /**
     * 外盘指数
     * @return
     */
    @GetMapping("/external/index")
    public R<List<OuterMarketDomain>> externalIndex(){
        return stockService.externalIndex();
    }
    /**
     * 板块指数
     *
     * @return
     */
    @GetMapping("/sector/all")
    public R<List<StockBlockRtInfoDomain>> sectorAll() {
        return stockService.sectorAllLimit();
    }

    /**
     * 涨幅榜
     *
     * @return
     */
    @GetMapping("/stock/increase")
    public R<List<StockUpDownCountDomain>> stockIncrease() {
        return stockService.stockIncreaseLimit();
    }

    /**
     * 涨幅榜 查看更多
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/stock/all")
    public R<PageResult<StockUpDownCountDomain>> stockAllPage(Integer page, Integer pageSize) {
        return stockService.stockAllPage(page, pageSize);
    }

    /**
     * 涨停跌停数据统计
     *
     * @return
     */

    @GetMapping("/stock/updown/count")
    public R<Map> stockUpDownCount() {
        return stockService.stockUpDownCount();
    }

    /**
     * 将指定页的股票数据导出到excel
     *
     * @param response
     * @param page
     * @param pageSize
     */
    @GetMapping("/stock/export")
    public void stockExport(HttpServletResponse response, Integer page, Integer pageSize) throws IOException {

        //让浏览器下载文件 必须设置响应头 响应数据类型
        //1.设置响应数据的类型:excel   文件mime类型
        response.setContentType("application/vnd.ms-excel");
        //2. 设置响应数据的编码格式
        response.setCharacterEncoding("utf-8");
        //3.设置默认的文件名称
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("stockRt" + new Date(), "UTF-8");
        //设置默认文件名称
        response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xlsx");
        List<StockExcelDomain> stockExcelDomains = stockService.stockExport(page, pageSize);

        EasyExcel.write(response.getOutputStream(), StockExcelDomain.class).sheet("stockRt").doWrite(stockExcelDomains);

    }

    /**
     * 统计国内A股大盘T日和T-1日成交量对比
     *
     * @return
     */
    @GetMapping("/stock/tradevol")
    public R<StockTradevolDomain> stockTradevol() {
        return stockService.stockTradevol();
    }

    /**
     * 个股涨跌幅度区间统计
     *
     * @return
     */
    @GetMapping("/stock/updown")
    public R<StockUpDownDomain> stockUpDown() {
        return stockService.stockUpDown();
    }

    /**
     * 个股分时K线行情
     * @param code
     * @return
     */
    @GetMapping("/stock/screen/time-sharing")
    public R<List<Stock4MinuteDomain>> stockScreenTimesharing(String code){
        return stockService.stockScreenTimesharing(code);
    }

    /**
     * 个股日K线详情
     * @param code
     * @return
     */
    @GetMapping("/stock/screen/dkline")
    public R<List<Stock4EvrDayDomain>> stockScreenDkline(String code){
        return stockService.stockScreenDkline(code);
    }

    /**
     * 周k
     * @return
     */
    @GetMapping("/stock/screen/weekkline")
    public R<List<Stock4WeekDomain>> StockScreenWeekKline(String code){
        return stockService.StockScreenWeekKline(code);
    }
}
